/*
 *  testClassTableMgr.cpp
 *  kangmodb
 *
 *  Created by 강모 김 on 11. 5. 8..
 *  Copyright 2011 강모소프트. All rights reserved.
 *
 */

#include "gtest/gtest.h"

#include "testCommon.h"
#include "types.h"
#include "kdInfra.h"
#include "tableMgr.h"	
	

/** @brief Test stgTableMgr class.
 */
class TableMgrTest : public testing::Test {
	#define TBL_REGION_NAME KD_STRING("./utest_table.region")
	#define TBL_REGION_MAP_ADDR ((void*)0x3000000000000000)
	static const u32 TBL_REGION_CHUNK_SIZE=1024; // 8MB
	static const u32 TBL_MAX_CHUNK_COUNT=3; // 8MB * 256 = 2GB
	static const stgTableId MAX_TABLE_COUNT=2;
	
protected:  // You should make the members protected s.t. they can be
	// accessed from sub-classes.
	
	// virtual void SetUp() will be called before each test is run.  You
	// should define it if you need to initialize the varaibles.
	// Otherwise, this can be skipped.
	virtual void SetUp() {
		tableMgr_ = new stgTableMgr();
		ASSERT_TRUE( tableMgr_ != NULL );
		ASSERT_TRUE( tableMgr_->initialize(TBL_REGION_NAME, TBL_REGION_MAP_ADDR, TBL_REGION_CHUNK_SIZE, TBL_MAX_CHUNK_COUNT, MAX_TABLE_COUNT ) == 0 );
	}
	
	// virtual void TearDown() will be called after each test is run.
	// You should define it if there is cleanup work to do.  Otherwise,
	// you don't have to provide it.
	//
	virtual void TearDown() {
		ASSERT_TRUE( tableMgr_->destroy() == 0 );
		delete tableMgr_;
		tableMgr_ = NULL;
	}
protected:
	stgTableMgr * tableMgr_;
};

/** @brief Basic tests. Normal Processing Scenario.
 */
TEST_F(TableMgrTest, DISABLED_Basic_NormalProcessing) {
	stgTableId table1_Id;
	stgTableId table2_Id;
	stgTableId table3_Id;
	stgTable * table1;
	stgTable * table2;
	// Repeat two times : create two tables, drop two tables
	for (int i=0; i<2; i++)
	{
		// Create two tables
		ASSERT_TRUE( tableMgr_->newTable( & table1_Id ) == 0 );
		ASSERT_TRUE( tableMgr_->getTable( table1_Id, & table1) == 0 );

		ASSERT_TRUE( tableMgr_->newTable( & table2_Id ) == 0 );
		ASSERT_TRUE( tableMgr_->getTable( table2_Id, & table2) == 0 );

		// Create 3rd table, hits the maximum table count limit 2.
		ASSERT_TRUE( tableMgr_->newTable( & table3_Id ) == KD_EXCP_HIT_MAX_TABLE_COUNT ); 

		// Drop the 1st table.
		ASSERT_TRUE( tableMgr_->deleteTable( table1_Id ) == 0 );
		// Drop the 2nd table.
		ASSERT_TRUE( tableMgr_->deleteTable( table2_Id ) == 0 );
	}
}

/** @brief Basic tests. Restart Recovery Scenario.
 */
TEST_F(TableMgrTest, DISABLED_Basic_RestartRecovery) {
	stgTableId table1_Id;
	stgTableId table2_Id;
	stgTableId table3_Id;
	stgTable * table1;
	stgTable * table2;
	// Repeat two times : create two tables, drop two tables
	for (int i=0; i<2; i++)
	{
		// Create two tables
		ASSERT_TRUE( tableMgr_->newTable( (table1_Id=1) ) == 0 );
		ASSERT_TRUE( tableMgr_->getTable( table1_Id, & table1) == 0 );
	
		ASSERT_TRUE( tableMgr_->newTable( (table2_Id=2) ) == 0 );
		ASSERT_TRUE( tableMgr_->getTable( table2_Id, & table2) == 0 );

		// Create 3rd table, hits the maximum table count limit 2.
		ASSERT_TRUE( tableMgr_->newTable( (table3_Id=3) ) == KD_EXCP_HIT_MAX_TABLE_COUNT ); 

		// Drop the 1st table.
		ASSERT_TRUE( tableMgr_->deleteTable( table1_Id ) == 0 );
		// Drop the 2nd table.
		ASSERT_TRUE( tableMgr_->deleteTable( table2_Id ) == 0 );
	}
}


